package com.lft.spring5.test;

import com.lft.spring5.annotation.AutoWired;
import com.lft.spring5.controller.UserController2;
import org.junit.Test;

import java.util.stream.Stream;

/**
 * Function: 		ADD FUNCTION.
 * Reason:   		ADD REASON.
 * Date:            2020-12-06 16:33
 * <p>
 * Class Name:      MyTest2
 * Package Name:    com.lft.spring5.test
 * @author Laifengting / E-mail:laifengting@foxmail.com
 * @version 1.0.0
 * @since JDK 8
 */
public class MyTest2 {
	@Test
	public void test() throws Exception {
		// 创建 Controller 对象
		UserController2 userController = new UserController2();
		// 获取 Controller 对象的类对象
		Class<? extends UserController2> clazz = userController.getClass();
		// 获取所有属性
		Stream.of(clazz.getDeclaredFields()).forEach(field -> {
			// 获取注解
			AutoWired annotation = field.getAnnotation(AutoWired.class);
			// 如果注解不为空。
			if (annotation != null) {
				// 设置私有属性可以访问
				field.setAccessible(true);
				try {
					// 通过获取属性的类型，通过属性类型创建实例对象
					Object o = field.getType().newInstance();
					System.out.println(o);
					// 通过属性设置，将实例对象赋值给userController的属性
					field.set(userController, o);
				} catch (IllegalAccessException | InstantiationException e) {
					e.printStackTrace();
				}
			}
		});
		System.out.println(userController.getUserService());
	}
	
}
